package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.PageList;
import cn.itsource.crm.domain.Employee;
import cn.itsource.crm.domain.MyUser;
import cn.itsource.crm.domain.Settlement;
import cn.itsource.crm.query.SettlementQuery;
import cn.itsource.crm.service.IEmployeeService;
import cn.itsource.crm.service.ISettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/settlement")
@Api(tags = "结算单类")
@CrossOrigin
public class SettlementController {

    @Autowired
    private ISettlementService settlementService;

    @Autowired
    private IEmployeeService employeeService;

    //分页查询方法
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="分页查询结算单",notes = "queryAll查询方法")
    @CrossOrigin
    public PageList queryPage(@RequestBody SettlementQuery settlementQuery){
        System.out.println(settlementQuery);
        MyUser user = (MyUser) SecurityUtils.getSubject().getPrincipal();
        //如果不是超级管理员
        if (user.getId() != 1L) {
            //将shiro中的结算员id封装到settlementQuery
            settlementQuery.setId(user.getId());
        }
        return settlementService.queryPage(settlementQuery);
    }

    @RequestMapping(value = "/queryAftermarketId",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询所有售后",notes = "queryAll查询方法")
    @CrossOrigin
    public List<Employee> queryByRoleId(){
        System.out.println("patch请求");
        return employeeService.queryByRoleId(4L);
    }

    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询结算单",notes = "queryAll查询方法")
    @CrossOrigin
    public List<Settlement> queryAll(){
        System.out.println("patch请求");
        return settlementService.queryAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询类型",notes = "queryOne查询方法")
    @ResponseBody
    public Settlement getOne(@PathVariable Long id){
        return  settlementService.queryOne(id);
    }


    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存结算单方法",notes = "save方法")
    public AjaxResult save(@RequestBody  Settlement settlement){
        try {
            System.out.println("接收的settlement对象:");
            settlementService.save(settlement);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value="根据id删除结算单",notes = "delete方法")
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);
            settlementService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改结算单方法",notes = "update方法")
    public AjaxResult update(@RequestBody  Settlement settlement){
        try {
            System.out.println("修的对象:"+settlement);
            System.out.println(settlement.getId());
            settlementService.update(settlement);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }


    @RequestMapping(value = "/saveAid",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存结算单方法",notes = "save方法")
    public AjaxResult saveAid(@RequestBody  Settlement settlement){
        try {
            System.out.println("接收的settlement对象:");
            settlementService.saveAid(settlement);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
}
